//
//  YMClassifyView.swift
//  YMSwiftUI
//
//  Created by anscen on 2022/3/14.
//

import SwiftUI

struct YMClassifyView: View {
    @State var titleName :String = ""
    @State var listData :[YMClassifyItem] = []
    
    @State var listAdap :[GridItem] = [GridItem(.adaptive(minimum: grid_width, maximum: grid_width + 15), spacing: 0, alignment: .center)]
    @State private var refreshData = BaseRefreshData()
    @State private var width  = grid_width
    var body: some View {
        GeometryReader { root in
            VStack(alignment: .center, spacing: 0) {
                ScrollView {
                    RefreshHeader(refreshing: $refreshData.refreshHeader, action: loadRefreshAction) { progress in
                        BaseRefreshHeaderView(refreshing: refreshData.refreshHeader)
                    }
                    LazyVGrid(columns: listAdap, spacing: 0){
                        ForEach(listData, id: \.self){
                            item in
                            YMGridCell(item: item,group: titleName,width: width)
                        }
                    }
                    RefreshFooter(refreshing: $refreshData.refreshFooter, action: loadMoreAction) {
                        BaseRefreshFooterView(noMoreData: refreshData.noMoreData,refreshing: refreshData.refreshFooter)
                    }.preload(offset: 50).noMore(refreshData.noMoreData)
                }.enableRefresh().onAppear {
                    debugPrint(root.safeAreaInsets)
//                    if root.size.width > 0{
//                        width  = (root.size.width - 60) / 3 - 1
//                        debugPrint(width)
//                        listAdap =  [GridItem(.adaptive(minimum: width, maximum: width + 15), spacing: 0, alignment: .center)]
//                    }
                }
            }.onAppear {
                
                self.loadData()
            }
        }
    }
    func loadMoreAction(){
        self.loadData()
    }
    func loadRefreshAction(){
        self.loadData()
    }
    func loadData(){
        ApiMoya.request(target: .classify) { json in
            self.listData.removeAll()
            let datas = [YMClassifyItem].deserialize(from: json[self.titleName].rawString())
            if let list = datas as? [YMClassifyItem]{
                self.listData = list
            }
            refreshData.endRefresh(true)
        } failure: { error in
            refreshData.endFailure(error)
        }
    }
}

struct YMClassifyCell :View{
    @State var group :String
    @State var item  :YMClassifyItem
    var body: some View{
        VStack(alignment: .leading, spacing: 0) {
            HStack(alignment: .top, spacing: 15) {
                WebImage(url: URL(string: item.cover)).resizable().placeholder {
                    Circle().foregroundColor(.gray)
                }.indicator(.activity).transition(.fade).frame(width: 70, height: 105, alignment: .center).padding(EdgeInsets.init(top: 12, leading: 0, bottom: 12, trailing: 0))
                HStack {
                    VStack(alignment: .leading, spacing: 10) {
                        Text("\(item.name)").foregroundColor(.black).padding(EdgeInsets.init(top: 15, leading: 0, bottom: 0, trailing: 0)).font(.system(size: 17)).lineLimit(1)
                        Text("书本总数:\(item.bookCount)").foregroundColor(.gray).font(.system(size: 14)).lineLimit(1)
                        Text("月票总数:\(item.monthlyCount)").foregroundColor(.yellow).font(.system(size: 14)).lineLimit(2)
                    }.padding(EdgeInsets.init(top: 12, leading: 0, bottom: 12, trailing: 0))
                }
            }
        }
    }
}
